#include "Game.h"

namespace core
{

Game::Game(uint8 num_of_players, unsigned int dealer)
	: mPlayersNum(num_of_players)
	, mDealer(dealer % num_of_players)
	, mRound(0)
	, mCurrentPlayer(dealer)
{
	core::Card* card_iter = mStartHand + NUM_OF_CARDS_IN_START_HAND * MAX_PLAYERS;
	while (card_iter != mStartHand)
		*(--card_iter) = Card(0,0);
	set_curr_player_ptr();
}

void Game::deal_start_hands()
{
	core::Card* card_iter = mStartHand;
	core::Card* end = mStartHand + NUM_OF_CARDS_IN_START_HAND * num_of_players();

	while (card_iter != end)
		*card_iter++ = mDeck.deal_card();

}


void Game::next_round()
{
	mRound++;
	next_player();
	set_curr_player_ptr();

	std::cout << "PLAYER " << curr_player_number() << " TURNS" << std::endl;

	if (!curr_player_ptr()->layout())
	{
		Card* card_iter = mStartHand + curr_player_number() * NUM_OF_CARDS_IN_START_HAND;
		Card* e = card_iter + NUM_OF_CARDS_IN_START_HAND;
		while (card_iter != e)
			curr_player_ptr()->make_turn(*card_iter++);
		curr_player_ptr()->set_layout();
	}
	else
	{
		curr_player_ptr()->make_turn(mDeck.deal_card());
	}
}

bool Game::end() const
{
	for (int n = 0; n < num_of_players(); n++)
		if (!mPlayers[n].hand().full())
			return false;
	return true;
}

void Game::output()
{
	//std::cout.clear();
	system("CLS");

	std::cout << "Game 1. Round " << round() << std::endl << std::endl;

	for (int player_number = 0; player_number < num_of_players(); player_number++)
	{
		std::cout << "Player " << player_number << ":\n\n";
		std::cout << mPlayers[player_number].hand();
		if (!mPlayers[player_number].layout())
		{
			std::cout << "Start hand: ";
			for (int i = 0; i < NUM_OF_CARDS_IN_START_HAND; i++)
				std::cout << mStartHand[NUM_OF_CARDS_IN_START_HAND * player_number + i] << ' ';
			std::cout << std::endl;
		}
		if (mPlayers[player_number].hand().full())
		{

			mPlayers[player_number].finish_hand();
			for (int bx_n = 0; bx_n < NUM_OF_BOXES_IN_HAND; bx_n++)
				std::cout << "box " << bx_n << " power - "
					<< mPlayers[player_number].hand().box_power(bx_n) << std::endl;
			std::cout << "Hand is ready! " <<
				((mPlayers[player_number].hand().valid())? "(valid)" : "INVALID!") << std::endl;

		}
		//std::cout << "Cards in hand: " << mPlayers[player_number].hand().count() << std::endl;

		std::cout << "\n----------------------------------------\n\n";
	}
}

void Game::results()
{
	//for (uint8 curr_pl_num = 0; curr_pl_num < num_of_players(); curr_pl_num++)
	//	mPlayers[curr_pl_num].hand().powers_count();

	for (uint8 curr_pl_num = 0; curr_pl_num < num_of_players(); curr_pl_num++)
		for (uint8 vs_pl_num = 0; vs_pl_num < num_of_players(); vs_pl_num++)
			if (curr_pl_num != vs_pl_num)
			{
				for (uint8 box_num = 0; box_num < NUM_OF_BOXES_IN_HAND; box_num++)
				{
					if (mPlayers[curr_pl_num].hand().box_power(box_num) >
						mPlayers[vs_pl_num].hand().box_power(box_num))
							mPlayers[curr_pl_num].add_cash(2);
					else if (mPlayers[curr_pl_num].hand().box_power(box_num) <
							 mPlayers[vs_pl_num].hand().box_power(box_num))
							 mPlayers[curr_pl_num].add_cash(-2);
				}
								
			}
}

void Game::results_output()
{
	std::cout << "CASH RESULTS:" << std::endl;
	for (uint8 curr_pl_num = 0; curr_pl_num < num_of_players(); curr_pl_num++)
		std::cout << "Player " << curr_pl_num << " ~ "
			<< mPlayers[curr_pl_num].cash() << std::endl;
}

} // namespace core